package com.tengroup.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.mail.MailException;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Random;

@Service
public class EmailService {

    @Autowired
    private JavaMailSender mailSender;

    // 生成6位数验证码
    public boolean sendEmail(String toEmail, String subject, String text) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setTo(toEmail);  // 设置收件人
            message.setSubject(subject);  // 设置邮件主题
            message.setText(text);  // 设置邮件内容

            mailSender.send(message);  // 发送邮件
            return true;  // 返回发送成功
        } catch (Exception e) {
            e.printStackTrace();  // 打印异常信息
            return false;  // 发送失败时返回 false
        }
    }
    }

